<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="1024"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<sc-VxeTable
			ref="save"
			:gridOps="gridOps"
			:gridEvts="gridEvts"
			:data="details"
			:tablecode="'storage_scrap_save'"
		>
		</sc-VxeTable>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()"
				>保 存</el-button
			>
		</template>
	</el-dialog>
	<skBrowserChoose
		v-model="openChooseDlg"
		:name="'storage_browser'"
		@close="openChooseDlg = false"
		@select="additems"
		:params="{ dyelot: { type: 'ne', value: '' } }"
	>
	</skBrowserChoose>
</template>

<script>
import skBrowserChoose from "@/components/skBrowserChoose";

export default {
	emits: ["success", "closed"],
	components: { skBrowserChoose },
	data() {
		return {
			mode: "add",
			openChooseDlg: false,
			plan_data: {},
			gridOps: {
				columns: [
					{
						field: "auxiliary_quantity",
						event: {
							keyup: ({ row }) => {
								row["quantity"] =
									row["auxiliary_quantity"] * 25;
								this.computeHank(row);
							},
						},
					},
					{
						field: "quantity",
						event: {
							keyup: ({ row }) => {
								this.computeHank(row);
							},
						},
					},
				],
				proxyConfig: null,
				height: 300,
			},
			details: [],
			gridEvts: {
				choose: () => (this.openChooseDlg = true),
			},
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			visible: false,
			isSaveing: false,
		};
	},
	mounted() {},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		async computeHank(row) {
			if ((this.plan_data[row.dyelot] ?? "") == "") {
				let res = await this.getDyelotPlan(row.dyelot);
				this.plan_data[row.dyelot] = res.data.rows[0] ?? {
					hank: 0,
					plan_quantity: 1,
				};
			}
			let dye_plan = this.plan_data[row.dyelot];
			row.hank = parseInt(
				(row.quantity / dye_plan.plan_quantity) * dye_plan.hank
			);
			row.feeding_ratio = dye_plan.feeding_ratio;
			row.yarn_quantity = (row.quantity * row.feeding_ratio) / 1000;
		},
		//表单提交方法
		async submit() {
			this.isSaveing = true;
			var res = await this.$API.storage.scrap.saveArray.post({
				details: this.details,
			});
			this.isSaveing = false;
			if (res.code == 200) {
				this.$emit("success", this.form, this.mode);
				this.visible = false;
				this.$message.success("操作成功");
			}
		},
		additems(items) {
			this.openChooseDlg = false;
			items.map(async (row) => {
				row["quantity"] = parseFloat(row.available_quantity);
				row["auxiliary_quantity"] = parseFloat(
					row.available_auxiliary_quantity
				);
				row.id = "key" + row.id;
				delete row.in_quantity;
				delete row.in_auxiliary_quantity;
				delete row.occupy_quantity;
				delete row.occupy_auxiliary_quantity;
				delete row.out_quantity;
				delete row.out_auxiliary_quantity;
				delete row.available_quantity;
				delete row.available_auxiliary_quantity;
				delete row.unit_id;

				this.computeHank(row);
				this.details.push(row);
			});
		},
		async getDyelotPlan(dyelot) {
			return await this.$API.pro.plan.list.get({
				filter: [{ dyelot: dyelot, plan_id: 1 }],
			});
		},
		//表单注入数据
		setData(data) {
			this.details.push(this.$TOOL.removeNull(data));
		},
	},
};
</script>

<style></style>
